/* 
 * H5Action.js
 */
import { saveData,getPreviewContent,getH5Data,createH5,copyH5 } from '../../api/H5Edit';
import { h5list,h5delete,updateTitle,h5ModalList,h5CategoryList } from '../../api/H5List';
import { getH5PublicInfo,publicH5,offlineH5,setModal,deleteModal } from '../../api/H5PublicSet';
import { getFormDataList } from '../../api/H5FormData';

import {BLANK_H5_DATA} from '../../utils/H5constants';
import {createNewPage} from '../../utils/H5helper';

//获取当前用户的H5列表数据
export function listH5Data(minId=null,limit=10){
	return dispatch=>{
		dispatch({type:'H5_LIST_DATA_START'});
		h5list(minId,limit).then(res=>{
			dispatch({type:'H5_LIST_DATA',datalist:res});
		}).catch(e=>{
			dispatch({type:'H5_LIST_DATA_END'});
			console.log('action listH5Data error');
			console.log(e);
		});
	}
}

//清空当前用户的列表数据
export function clearListH5Data(){
	return {type:'CLEAR_H5_LIST_DATA'};
}

//创建新的h5页面
export function createNewH5(callback=null){
	return dispatch=>{
		let pageList=Array();
		const newPage=createNewPage(1).toJS();
		newPage.components=[];
		pageList[0]=newPage;
		createH5({attr:JSON.stringify(BLANK_H5_DATA.data.attributes),pagesData:JSON.stringify(pageList)})
		.then(res=>{
			const firstPageId=res.page_datas[0].id;
			dispatch({type:'INIT_DATA',
						id:res.id,
						uniqueCode:res.unique_code,
						title:res.title,
						firstPageId:firstPageId,
						attributes:res.attributes,
						pagesData:res.page_datas});
			if(callback){
				callback(res.unique_code);
			}
			
		}).catch(e=>{
			console.log('action create h5 data error');
			console.log(e);
		});
	}
}

//根据模版创建新h5
export function copyNewH5(id,callback=null){
	return dispatch=>{
		copyH5({id:id})
		.then(res=>{
			const firstPageId=res.page_datas[0].id;
			dispatch({type:'INIT_DATA',
						id:res.id,
						uniqueCode:res.unique_code,
						title:res.title,
						firstPageId:firstPageId,
						attributes:res.attributes,
						pagesData:res.page_datas});
			if(callback){
				callback(res.unique_code);
			}
			
		}).catch(e=>{
			console.log('action copy h5 data error');
			console.log(e);
		});
	}
}

//查询初始化h5页面数据
export function getInitPageData(uniqueCode){
	return dispatch=>{
		getH5Data({uniqueCode:uniqueCode})
		.then(res=>{
			const firstPageId=res.page_datas?res.page_datas[0].id:null;
			dispatch({type:'INIT_DATA',
						id:res.id,
						uniqueCode:res.unique_code,
						title:res.title,
						firstPageId:firstPageId,
						attributes:res.attributes,
						pagesData:res.page_datas});
			
		}).catch(e=>{
			console.log('action load h5 init data error');
			console.log(e);
		});
	}
	
}

//保存h5的编辑数据
export function saveAllDatas(datas,callback=null){
	return dispatch=>{
		const pageDatas=datas.data.pages;
		let pageList=Array();
		pageDatas.forEach(function (item) {
	  		const idx=item.get('pageNum')-1;
	  		const components=item.get('components');
	  		let compList=Array();
	  		components.forEach(function (com) {
	  			const idx2=com.get('compNum')-1;
	  			compList[idx2]=com;
	  		});
	  		const newItem=item.set('components',compList);
	  		pageList[idx]=newItem.toJS();
		});
		saveData({id:datas.id,attr:JSON.stringify(datas.data.attributes),pagesData:JSON.stringify(pageList)})
		.then(res=>{
			dispatch({type:'SAVE_H5_DATA'});
			if(callback) callback();
		}).catch(e=>{
			console.log('action save data error');
			console.log(e);
		});
	}
	
}

//预览按钮加载预览页面的html数据
export function loadPreviewData(uniqueCode){
	return  getPreviewContent({uniqueCode:uniqueCode})
			.then(res=>{return res.data;})
			.catch(e=>{
				console.log('action load preview error');
				console.log(e);
			});
	/*return dispatch=>{
		getPreviewContent()
		.then(res=>{
			dispatch({type:'H5_PREVIEW_SET_DATA',data:res.data});
			if(callback) callback();
		})
		.catch(e=>{
			dispatch({type:'H5_PREVIEW_LOADING_END'});
			console.log('load preview error');
			console.log(e);
		});
	}*/
}

//修改整个h5页面的属性
export function upH5Attr(name,value){
	return {'type':'UPDATE_H5DATA_ATTR',name:name,value:value};
}

//删除整个H5
export function deleteThisH5(id){
	return dispatch=>{
		h5delete(id).then(res=>{
			dispatch({type:'H5_DETELE_H5_PAGE',id:id});
		}).catch(e=>{
			console.log('action delete h5 error');
			console.log(e);
		});
	}
}

//修改H5标题
export function updateH5Title(id,title){
	return dispatch=>{
		updateTitle(id,title).then(res=>{
			dispatch({type:'H5_UPDATE_TITLE',id:id,title:title});
		});
	}
}

//获取h5发布设置信息
export function h5PublicSetInfo(uniqueCode){
	return dispatch=>{
		getH5PublicInfo({uniqueCode:uniqueCode})
		.then(res=>{
			dispatch({type:'H5_PUBLIC_INFO_INIT',data:res});
		}).catch(e=>{
			console.log('action get public set information error');
			console.log(e);
		});
	}
}
//发布设置页面，发布信息内容的变更
export function h5PublicInfoChange(name,value){
	return {'type':'H5_PUBLIC_INFO_CHANGE',name:name,value:value};
}

//发布H5
export function h5PublicNow(submitData,callback=null){
	return dispatch=>{
		publicH5(submitData)
		.then(res=>{
			if(res.code==200){
				if(callback) callback(res.data);
			}
		})
		.catch(e=>{
			console.log('action public h5 error');
			console.log(e);
		});
	}
}
//下线h5
export function h5OfflineNow(id,callback=null){
	return dispatch=>{
		offlineH5({id:id})
		.then(res=>{
			if(res.code==200){
				dispatch({type:'H5_OFFLINE'});
				if(callback) callback();
				alert('下线成功');
			}
		})
		.catch(e=>{
			console.log('action offline h5 error');
			console.log(e);
		});
	}
}
//设置为模版
export function h5SetModal(id,catId,callback=null){
	return dispatch=>{
		setModal({id:id,catId:catId})
		.then(res=>{
			if(res.code==200){
				//dispatch({type:'H5_SET_MODAL_SUCCSS'});
				if(callback) 
					callback();
				else
					alert('设置模版成功');
			}
		})
		.catch(e=>{
			console.log('action set modal h5 error');
			console.log(e);
		});
	}
}
//删除模版
export function h5DeleteModal(id,callback=null){
	return dispatch=>{
		deleteModal({id:id})
		.then(res=>{
			if(res.code==200){
				//dispatch({type:'H5_DELETE_MODAL_SUCCSS'});
				if(callback) callback();
			}
		})
		.catch(e=>{
			console.log('action delete modal h5 error');
			console.log(e);
		});
	}
}

//获取表单提交数据
export function h5FormDataList(uniqueCode,submitPage=null,id=null,limit=10){
	return dispatch=>{
		dispatch({type:'H5_LIST_FORM_DATA_START'});
		getFormDataList({uniqueCode:uniqueCode,id:id,limit:limit,submitPage:submitPage})
		.then(res=>{
			dispatch({type:'H5_LIST_FORM_DATA',datalist:res.datalist,submitPageNums:res.submit_page_nums});
		})
		.catch(e=>{
			dispatch({type:'H5_LIST_FORM_DATA_END'});
			console.log('action get h5 form submit data list error');
			console.log(e);
		});
	}
}
//加载更多表单提交数据
export function loadMoreH5FormData(uniqueCode,submitPage,id,limit=10){
	return dispatch=>{
		dispatch({type:'H5_LIST_FORM_DATA_START'});
		getFormDataList({uniqueCode:uniqueCode,id:id,limit:limit,submitPage:submitPage})
		.then(res=>{
			dispatch({type:'H5_LIST_FORM_MORE_DATA',datalist:res.datalist});
		})
		.catch(e=>{
			dispatch({type:'H5_LIST_FORM_DATA_END'});
			console.log('action load more h5 form submit data list error');
			console.log(e);
		});
	}
}

//清空表单提交数数据页面数据
export function clearH5FormData(){
	return {type:'CLEAR_H5_FORM_LIST_DATA'};
}

//获取模版列表
export function listH5ModalData(minId=null,limit=10,catId=''){
	return dispatch=>{
		dispatch({type:'H5_LIST_DATA_START'});
		h5ModalList(minId,limit,catId).then(res=>{
			dispatch({type:'H5_LIST_DATA',datalist:res});
		}).catch(e=>{
			dispatch({type:'H5_LIST_DATA_END'});
			console.log('action listH5ModalData error');
			console.log(e);
		});
	}
}

//获取H5分类列表
export function getH5Categories(){
	return dispatch=>{
		h5CategoryList().then(res=>{
			dispatch({type:'H5TPL_CATEGORY_LIST',datalist:res});
		}).catch(e=>{
			console.log('action h5CategoryList error');
			console.log(e);
		});
	}
}
